package newcoder_exp.justPractise;
import java.util.*;
import org.junit.*;
public class NC77 {
    public class Solution {
        //奇偶交换
        public int[] reOrderArray1 (int[] array) {
            int lo = -1, hi = 0, len = array.length;
            while (hi < len) {
                if ((array[lo + 1] & 1) == 1) {
                    lo++;
                } else {
                    hi = lo + 1;
                    while (hi < len && (array[hi] & 1) == 0) hi++;
                    if (hi == len) break;
                    else {
                        swap(array, ++lo, hi);
                    }
                }
            }
            return array;
        }

        public int[] reOrderArray (int[] array) {
            int len = array.length, count = 0;
            int[] res = new int[len];
            for (int i = 0; i < len; i++) 
                if ((array[i] & 1) == 1) count++;
            
            int lo = 0, hi = count;
            for (int i = 0; i < len; i++) 
                if ((array[i] & 1) == 1) res[lo++] = array[i];
                else res[hi++] = array[i];
            
            return res;
        }
        
        public void swap(int[] nums, int i, int j) {
            int temp = nums[i];
            nums[i] = nums[j];
            nums[j] = temp;
        }
    
    }

    @Test
    public void test() {
        Solution s = new Solution();
        int[] nums = new int[]{1,2,3,4};
        nums = new int[] {2,4,6,5,7};
        int[] res = s.reOrderArray(nums);
        System.out.println(Arrays.toString(res));
    }
}
